package com.jywl.controller;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jywl.entity.Column;
import com.jywl.entity.SingleConfigSet;
import com.jywl.entity.Table;
import com.jywl.mapper.GeneratorMapper;
import com.jywl.util.DownUtil;
import com.jywl.util.GeneratorUtil;
import com.jywl.util.JdbcUtil;


/** 
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2017-8-10 下午4:35:37 <br/>
 *
 * @author Sunxq
 * @version 
 * @since JDK 1.8
 */
@Controller
@RequestMapping("/cg")
public class GeneratorController {
	@Autowired
	private GeneratorMapper generatorMapper;	
	@RequestMapping("/single")
	public String single(String error,Model model) {
		/*String path =this.getClass().getClassLoader().getResource("/").getPath();
		String[] paths = path.split("/");
		String rp = paths[1]+"\\"+paths[2]+"\\"+paths[8];
		model.addAttribute("rp", rp);*/
		//JdbcUtil.query();
		return "cg/single";
	}
	@RequestMapping("/searchEntity")
	public String searchEntity(String error,Model model) {
		//JdbcUtil.query();
		return "cg/searchEntity";
	}
	@RequestMapping("/queryTables")  
	@ResponseBody
    public List<Table> queryTables(
    		@RequestParam HashMap<String, Object> params) {
	   return generatorMapper.queryTables(params);  
    } 
	@RequestMapping("/loadSql")  
	@ResponseBody
    public List<Column> loadSql(
    		@RequestBody HashMap<String, Object> params) {
	   return JdbcUtil.loadSql(params.get("sql").toString());  
    } 
	
	
	
	
	@RequestMapping(value="/queryColumns",produces="application/json")  
	@ResponseBody
    public List<Column> queryColumns(
    		@RequestBody HashMap<String, Object> params) {
		System.out.println(params);
	   return generatorMapper.queryColumns(params);  
    } 
	@RequestMapping(value="/generatorSingle",produces="application/json") 
	@ResponseBody
    public HashMap<String, String> generatorSingle(
    		@RequestBody SingleConfigSet params,HttpServletRequest request,  
            HttpServletResponse response) {
	   String filePath = GeneratorUtil.gcFile(params,1);
	  // DownUtil.download(filePath,request,response);
	   HashMap<String, String> res = new HashMap<>();
	   res.put("filePath", filePath);
	   return res;  
    }
	@RequestMapping(value="/generatorSearchEntity",produces="application/json") 
	@ResponseBody
    public HashMap<String, String> generatorSearchEntity(
    		@RequestBody SingleConfigSet params,HttpServletRequest request,  
            HttpServletResponse response) {
	   String filePath = GeneratorUtil.gcFile(params,2);
	  // DownUtil.download(filePath,request,response);
	   HashMap<String, String> res = new HashMap<>();
	   res.put("filePath", filePath);
	   return res;  
    }
	
	
	
}


